function Listener(eventType,listener,bus){
	var MinecraftForge=Java.type("net.minecraftforge.common.MinecraftForge");
	this.eventType=eventType;
	this.listener=listener;
	this.bus=(bus==null)?MinecraftForge.EVENT_BUS:bus;
	this.getEventType=function(){
		return this.eventType;
	}
	this.getListener=function(){
		return this.listener;
	}
	this.getBus=function(){
		return this.bus;
	}
}
function listenEvent(eventType,priority,listener,bus){
	var Event=Java.type(eventType);
	try{
		var IEventListener=Java.type("net.minecraftforge.fml.common.eventhandler.IEventListener");
		var EventPriority=Java.type("net.minecraftforge.fml.common.eventhandler.EventPriority");
	}catch(err){
		var IEventListener=Java.type("cpw.mods.fml.common.eventhandler.IEventListener");
		var EventPriority=Java.type("cpw.mods.fml.common.eventhandler.EventPriority");
	}
	var MinecraftForge=Java.type("net.minecraftforge.common.MinecraftForge");
	var event=new Event();
	if(priority=="LOW"){
		priority=EventPriority.LOW;
	}
	if(priority=="HIGH"){
		priority=EventPriority.HIGH;
	}
	if(priority=="NORMAL"){
		priority=EventPriority.NORMAL;
	}
	if(priority=="LOWEST"){
		priority=EventPriority.LOWEST;
	}
	if(priority=="HIGHEST"){
		priority=EventPriority.HIGHEST;
	}
	var EventListener=Java.extend(IEventListener,{
		invoke:listener
	});
	listener=new EventListener();
	var bus=(bus==null)?MinecraftForge.EVENT_BUS:bus;
	var busID=bus.class.getDeclaredField("busID");
	busID.setAccessible(true);
	busID=busID.getInt(bus);
	event.getListenerList().register(busID,priority,listener);
	return new Listener(eventType,listener,bus);
}
function unListenEvent(eventType,listener,bus){
	var Event=Java.type(eventType);
	var MinecraftForge=Java.type("net.minecraftforge.common.MinecraftForge");
	var event=new Event();
	var bus=(bus==null)?MinecraftForge.EVENT_BUS:bus;
	var busID=bus.class.getDeclaredField("busID");
	busID.setAccessible(true);
	busID=busID.getInt(bus);
	event.getListenerList().unregister(busID,listener);
}
//LivingUpdate net.minecraftforge.event.entity.living.LivingEvent.LivingUpdateEvent
var PlayerInteractEventAction =Java.type("net.minecraftforge.event.entity.player.PlayerInteractEvent.Action");
var EntityClientPlayerMP=Java.type("net.minecraft.client.entity.EntityClientPlayerMP");
var EntityPlayerMP = Java.type("net.minecraft.entity.player.EntityPlayerMP");
var ScriptPlayer=Java.type("noppes.npcs.scripted.ScriptPlayer");
listenEvent("net.minecraftforge.event.entity.living.LivingEvent.LivingUpdateEvent","NORMAL",function(event){
	if(event.entityLiving instanceof EntityClientPlayerMP){
		return;
	}
	try{

		if(event.entityLiving instanceof EntityPlayerMP){
			player = new ScriptPlayer(event.entityLiving);
			//npc.say(player.getName())
			if(player.getName() != playerName){
				return;
			}
			//走动部分
			var ox = npc.getTempData("LatestX");
			var oz = npc.getTempData("LatestZ");
			var oy = npc.getTempData("LatestY");
			npc.getMCEntity().field_70159_w = -(player.getX() - ox);
			npc.getMCEntity().field_70179_y = -(player.getZ() - oz);
			npc.getMCEntity().field_70181_x = player.getY() - oy;
			npc.getMCEntity().field_70133_I=true;
			npc.setTempData("LatestX",player.getX())
			npc.setTempData("LatestZ",player.getZ())
			npc.setTempData("LatestY",player.getY())
		}
        }catch(err){

		}
	
});
listenEvent("net.minecraftforge.event.entity.player.PlayerInteractEvent","HIGH",function(event){
	if(event.action == PlayerInteractEventAction.LEFT_CLICK_BLOCK || event.action == PlayerInteractEventAction.RIGHT_CLICK_BLOCK ){
		return;
	}
	if(event.entityLiving instanceof EntityClientPlayerMP){
		return;
	}
	try{

		if(event.entityLiving instanceof EntityPlayerMP){
			player = new ScriptPlayer(event.entityLiving);
			//npc.say(player.getName())
			if(player.getName() != playerName){
				return;
			}
			//玩家触发动作
			var poses = getCirclePoints(1,20)
			drawPos(poses,"lava")
			var ps = npc.getSurroundingEntities(1,1);
			for (var i = 0; i < ps.length; i++) {
				if(player.getHeldItem()==null){
					damageamount = 0;
				}else{
					damageamount=6
				}
				damage(ps[i],player)
			}
		}
	}catch(err){

	}
});
function drawPos(pos,particle){
	for(var i =0;i<pos.length;i++){
		world.spawnParticle(particle,pos[i][0],pos[i][1],pos[i][2],0.1,0.1,0.1,0,1);
	}
}
function getWorldPosFromOffsets(p,offsets){
	var pos = [];
	for (var i = 0; i < offsets.length; i++) {
		var offset =  offsets[i];
		pos[i]=[p.getX()+offset[0],p.getY()+offset[1],p.getZ()+offset[2]];
	}
	return pos;
}
function getCirclePoints(radius,count){
	var posoffset = [];
	var y=0;
	angle=(360*Math.PI/180) /count
	for(var i=0;i<count;i++){
		var x = radius* Math.cos(angle*(i+1));
		var z = radius*Math.sin(angle*(i+1));
		posoffset[i]=[x,y,z];
	}
	return posoffset;
}
function damage(target, type, amount, form) {
	var DamageSource =Java.type("net.minecraft.util.EntityDamageSource")
	target.getMCEntity().func_70097_a(new DamageSource(type,form.getMCEntity()), amount)
}
